package com.fireline.beauty;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.IDataBaseProvide;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.widgets.BatchItem;

public class SystemFunctionDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	private Text text_1;
	private Text text_2;
	private Text text_3;
	private Text text_4;
	/**
	 * @wbp.nonvisual location=63,592
	 */
	private final Batch addBatch = new Batch(SqliteJdbcImpl.INSTANCE);

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public SystemFunctionDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {

		BatchItem batchItem = new BatchItem(addBatch);
		batchItem
				.setCommand("insert into systemFunction (\"id\",\"type\",\"name\",\"describe\",\"image\",\"class\") \r\nvalues (?,?,?,?,?,?)");
		batchItem.setName("添加系统功能节点");
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX
				| SWT.APPLICATION_MODAL);
		shell.setSize(592, 435);
		shell.setText("系统功能设置");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(shell, SWT.NONE);

		Tree tree = new Tree(sashForm, SWT.BORDER);

		Menu menu = new Menu(tree);
		tree.setMenu(menu);

		MenuItem mntmNewItem = new MenuItem(menu, SWT.NONE);
		mntmNewItem.setText("新增&A");

		MenuItem mntmNewItem_1 = new MenuItem(menu, SWT.NONE);
		mntmNewItem_1.setText("上移&U");

		MenuItem mntmNewItem_2 = new MenuItem(menu, SWT.NONE);
		mntmNewItem_2.setText("下移&D");

		MenuItem mntmNewItem_3 = new MenuItem(menu, SWT.NONE);
		mntmNewItem_3.setText("删除&R");

		Composite composite = new Composite(sashForm, SWT.BORDER);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginHeight = 15;
		gl_composite.marginWidth = 15;
		gl_composite.verticalSpacing = 20;
		composite.setLayout(gl_composite);

		Label label = new Label(composite, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("功能编号：");

		text = new Text(composite, SWT.BORDER);
		text.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_1.setText("所属类别：");

		text_1 = new Text(composite, SWT.BORDER);
		text_1.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		text_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_2.setText("功能名称：");

		text_2 = new Text(composite, SWT.BORDER);
		text_2.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		text_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_3.setText("功能描述：");

		text_3 = new Text(composite, SWT.BORDER);
		text_3.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		text_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label label_4 = new Label(composite, SWT.NONE);
		label_4.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_4.setText("对应资源：");

		text_4 = new Text(composite, SWT.BORDER);
		text_4.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		text_4.setToolTipText("功能对应的IFunction 类");
		text_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label label_5 = new Label(composite, SWT.NONE);
		label_5.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label_5.setText("对应图标：");

		CLabel lblNewLabel = new CLabel(composite, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL,
				false, true));
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblNewLabel.setText("选择图标");
		new Label(composite, SWT.NONE);

		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, false,
				false, 1, 1));
		composite_1.setLayout(new GridLayout(2, false));

		Button btnNewButton_1 = new Button(composite_1, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});
		GridData gd_btnNewButton_1 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_1.heightHint = 25;
		gd_btnNewButton_1.widthHint = 80;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setText("保存&S");

		Button btnNewButton = new Button(composite_1, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doClear();
			}
		});
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.widthHint = 80;
		gd_btnNewButton.heightHint = 25;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("清空&C");
		sashForm.setWeights(new int[] { 215, 368 });
		setParams();

	}

	/*
	 * 保存
	 */
	protected void doSave() {
		setParams();
		try {
			addBatch.execute();
		} catch (Exception e) {
			e.printStackTrace();
			MessageDialog
					.openInformation(shell, "提示", "保存失败:" + e.getMessage());
		}
		MessageDialog.openInformation(shell, "提示", "保存成功");
	}

	/*
	 * 清空
	 */
	protected void doClear() {

	}

	protected void setParams() {
		addBatch.setParam(0, 0, text.getText());
		addBatch.setParam(0, 1, text_1.getText());
		addBatch.setParam(0, 2, text_2.getText());
		addBatch.setParam(0, 3, text_3.getText());
		addBatch.setParam(0, 4, "");
		addBatch.setParam(0, 5, text_4.getText());
	}

	public static void main(String[] args) {
		SystemFunctionDialog dlg = new SystemFunctionDialog(new Shell(),
				SWT.NONE);
		dlg.open();
	}
}
